<?php namespace nimo\domains\acc\controllers;

use BaseController;
use Input;
use View;
use Validator;
use Redirect;
use Request;
use Response;

use nimo\domains\acc\models\MasterCode as MasterCode;

class MasterCodeController extends BaseController {

    /**
     * MasterCode Repository
     *
     * @var MasterCode
     */
    protected $masterCode;

    public function __construct(MasterCode $masterCode)
    {
        $this->masterCode = $masterCode;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $master_codes = MasterCode::where('domain', '=', 0)->orderBy('node_left')->get();
        if (Request::ajax()) {
            return Response::json($master_codes);
        }
        $nested = array_map(function($masterCode){
            $a=$masterCode->toArray();
            $a['pkey']=$masterCode->getJointPk();return $a;
        }, $master_codes->all());
        $jsonMasterCode = json_encode($nested);
        return View::make('nimo.domains.acc.master.code.index', compact('master_codes', 'jsonMasterCode'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('nimo.domains.acc.master.code.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, MasterCode::$rules);

        if ($validation->passes())
        {
            $masterCode = null;
            $parentid = Input::get('node_parent');
            if ($parentid > 0 ) {
                $keys = array('domain'=>1, 'id'=>$parentid);
                $parent = MasterCode::find($keys);
                if ($parent != null) {
                    $data = Input::only('id', 'name');
                    $masterCode = $this->masterCode->create($input);
                    $masterCode->makeChildOf($parent);
                }
            } else {
                $masterCode = $this->masterCode->create($input);
            }
            if (!is_null($masterCode)) {
                return Redirect::route('acc.master.code.show', $masterCode->getJointPk())
                    ->with('success', 'There were successed.');
            }
            return Redirect::route('acc.master.code.create')
                ->withInput()
                ->with('error', 'There were node relation errors.');
        }
        return Redirect::route('acc.master.code.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $masterCode = $this->masterCode->findByJointPkOrFail($id);

        return View::make('nimo.domains.acc.master.code.show', compact('masterCode'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $masterCode = $this->masterCode->findByJointPk($id);

        if (is_null($masterCode))
        {
            return Redirect::route('acc.master.code.index');
        }

        return View::make('nimo.domains.acc.master.code.edit', compact('masterCode'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, MasterCode::$rules);

        if ($validation->passes())
        {
            $masterCode = $this->masterCode->findByJointPk($id);
            $parentid = Input::get('node_parent');
            $masterCode->update($input);
            if (!empty($parentid) && $masterCode->node_parent != $parentid) {
                // change parent
                $masterCode->makeChildOf($parentid);
            } else if(empty($parentid) && !empty($masterCode->node_parent)){
                // root
                $masterCode->makeRoot();
            }
            return Redirect::route('acc.master.code.show', $id)
                ->with('success', 'There were successed.');
        }

        return Redirect::route('acc.master.code.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->masterCode->findByJointPk($id)->delete();

        return Redirect::route('acc.master.code.index');
    }

    /**
     * move up
     *
     * @param  int  $id
     * @return Response
     */
    public function up($id)
    {
        $masterCode = $this->masterCode->findByJointPk($id);
        if (!is_null($masterCode)) {
            $node = $masterCode->getLeftSibling();
            if (!is_null($node)) {
                $masterCode =  $masterCode->moveToLeftOf($node);
                return Redirect::route('acc.master.code.index')
                    ->with('success', 'There were successed.');
            }
        }

        return Redirect::route('acc.master.code.index');
    }

    /**
     * move down
     *
     * @param  int  $id
     * @return Response
     */
    public function down($id)
    {
        $masterCode = $this->masterCode->findByJointPk($id);
        if (!is_null($masterCode)) {
            $node = $masterCode->getRightSibling();
            if (!is_null($node)) {
                $masterCode =  $masterCode->moveToRightOf($node);
                return Redirect::route('acc.master.code.index')
                    ->with('success', 'There were successed.');
            }
        }

        return Redirect::route('acc.master.code.index');
    }

}